<?php
namespace app\index\controller;
use app\common\Rsa;
use think\Controller;
use think\facade\Session;

/*控制界面显示类*/
class Index extends Controller
{

    // TODO 主页肯定是要写的
    /*-------------------------------网站入口-------------------------*/
    public function index()
    {
        /*-----session验证与数据传输---*/
        if (Session::has("login") && Session::get("login") != "admin"){
            $this->redirect("/user");
        }else if(Session::get("login")=="admin"){
            $this->redirect("/admin");
        }else{
            $image = new Image();
            $this->assign("imageClass", "最新热榜")
                ->assign('imageCounts', $image->countImage())
                ->assign('title','拾缘图床')
                ->assign('image',$image->selectFirstImage());
            return $this->fetch("index");
        }
    }

    /*------------------------------登录检验----------------------------*/
    /*-----post 登录检验------------*/
    public function Login(){
        /*检查登录身份*/
        return json($this->request->post());;
    }

    /*---控制显示登录界面----*/
    public function showLogin($hint)
    {
        if($hint=="2"){
            $this->assign("hint", "重新登录");
        }else if($hint == "1"){
            $this->assign("hint", "验证码错误");
        }else{
            $this->assign("hint", "PhotoShare");
        }
        $this->assign("username","hello");
        $this->assign('title','Login');
        return $this->fetch("index/login");
    }

    /*-----------------------------注册页面------------------------------*/
    public function showRegister()
    {
        $this->assign('title','User|Register');
        $this->assign('hint','PhotoShare');
        return $this->fetch("index/register");
    }

    public function registerThis()
    {
        $registerData = array(
            'member_name'  => $this->request->post("nickname"),
            'member_email'     => $this->request->post("email"),
            'member_password' => Rsa::privDecrypt($this->request->post("password2"))
        );

        $member = new Member();
        $code = new Code();
        /*验证码*/
        if($code->checkCode()){
            $mess = $member->registerThisInBase($registerData);
        }else{
            return $this->assign("hint","验证码错误")->assign("title","User|Register")->fetch("index/register");
        }
        if($mess == "1"){
            /*注册成功*/
            $this->redirect("/login/0");
        }
        if ($mess == "3"){
            return $this->assign("hint","邮箱已用")->assign("title","User|Register")->fetch("index/register");
        }
        if ($mess == "4"){
            return $this->assign("hint","昵称已用")->assign("title","User|Register")->fetch("index/register");
        }
        /*注册失败*/
        return $this->assign("hint","注册失败")->assign("title","User|Register")->fetch("index/register");
    }

    /*----readme-----*/
    public function readme(){
        $this->assign("title","Readme");
        return $this->fetch("index/readme");
    }
    /*-----downloadApp*/
    public function DownloadApp(){
        $this->assign("title","下载app");
        return $this->fetch("index/download_app");
    }

    public function task(){
        return $this->fetch("index/task");
    }
}
